import { defineStore } from 'pinia'
import { login, logOffs } from '@/api/admin'
import { tagsStore } from '@/store/tags'
import router from '@/router/router'
export const tokenStore = defineStore('tokenStore', {
  state: () => ({
    token: ''
  }),
  actions: {
    async setToken(data: any) {
      await login(data).then(res => {
        this.token = res.data || ''
      })
    },
    async logOff() {
      if (this.token) {
        logOffs()
      }
      const routers = router.getRoutes()
      if (routers) {
        routers.map(it => {
          if (!['login', 'home', '个人中心', 'router', '500', '404', '403'].includes(it.name)) {
            router.removeRoute(it.name)
          }
        })
      }
      tagsStore().removeAll()
      this.token = ''
      router.push('/login')
    }
  },
  persist: true
})
